<script setup lang="ts">
import {onMounted, ref} from "vue";
import {getCurrentUser} from "../../services/user";
import {Toast} from "vant";
import {useRouter} from "vue-router";
import myAxios from "../../api/myAxios";

const oldPassword = ref('');
const newPassword = ref('');
const newCheckPassword = ref('');
const user = ref();
const router = useRouter();

onMounted(async () => {
  let currentUser = await getCurrentUser();
  if (currentUser) {
    user.value = currentUser
  } else {
    Toast("未登录")
    await router.replace("/user/login")
  }
})

const onSubmit = () => {
  myAxios.post('/user/resetPassword', {
    oldPassword: oldPassword.value,
    newPassword: newPassword.value,
    newCheckPassword: newCheckPassword.value
  }).then(res => {
    if (res?.code === 0 && res.data) {
      Toast.success('修改成功');
      router.push('/user/login');
    } else {
      Toast.fail('修改失败' + (res.description ? `，${res.description}` : ''));
    }
  });
};
</script>

<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="oldPassword"
          type="password"
          label="旧密码"
          placeholder="请输入旧密码"
          :rules="[{ required: true, message: '请输入旧密码' }]"
      />
      <van-field
          v-model="newPassword"
          type="password"
          label="新密码"
          placeholder="请输入新密码"
          :rules="[{ required: true, message: '请输入新密码' }]"
      />
      <van-field
          v-model="newCheckPassword"
          type="password"
          label="确认新密码"
          placeholder="请确认新密码"
          :rules="[{ required: true, message: '请确认新密码' }]"
      />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        修改
      </van-button>
    </div>
  </van-form>

</template>

<style scoped>

</style>
